﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreMVVM.ViewModels;
using GeometryViz3D.Model;
using System.Windows.Input;
using CoreMVVM.Commands;
using System.Windows.Media.Media3D;
using System.Windows;

namespace GeometryViz3D.ViewModels
{
    public class AddPointViewModel : ViewModelBase
    {
        #region Fields

        G3DModel m_model;

        private string m_x, m_y, m_z;
        private string m_label;

        private DelegateCommand m_AddPointCommand;

        #endregion

        public AddPointViewModel(G3DModel model)
        {
            m_model = model;

            m_x = m_y = m_z = "0";
        }

        #region Properties

        public G3DModel Model
        {
            get { return m_model; }
            set { m_model = value; }
        }

        public string X
        {
            get { return m_x; }
            set
            {
                m_x = value;
                OnPropertyChanged("X");
            }
        }

        public string Y
        {
            get { return m_y; }
            set
            {
                m_y = value;
                OnPropertyChanged("Y");
            }
        }

        public string Z
        {
            get { return m_z; }
            set
            {
                m_z = value;
                OnPropertyChanged("Y");
            }
        }

        public string Label
        {
            get { return m_label; }
            set
            {
                m_label = value;
                OnPropertyChanged("Label");
            }
        }

        #region Commands

        public ICommand AddPointCommand
        {
            get
            {
                if (m_AddPointCommand == null)
                {
                    m_AddPointCommand = new DelegateCommand(AddPoint, CanAddPoint);
                }

                return m_AddPointCommand;
            }
        }

        #endregion

        #endregion

        #region Event Handlers

        private bool CanAddPoint()
        {
            return !string.IsNullOrEmpty(m_label) &&
                !string.IsNullOrEmpty(m_x) && 
                !string.IsNullOrEmpty(m_y) && 
                !string.IsNullOrEmpty(m_z);
        }

        private void AddPoint()
        {
            try
            {
                m_model.AddPoint(m_label, GetPoint());
                OnPropertyChanged("Model");
            }
            catch (G3DException ex)
            {
                MessageBox.Show(ex.Message, "GeometryViz3D");
            }
        }

        private Point3D GetPoint()
        {
            double xx, yy, zz;

            if (double.TryParse(m_x, out xx) &&
                double.TryParse(m_y, out yy) &&
                double.TryParse(m_z, out zz))
            {
                return new Point3D(xx, yy, zz);
            }
            else
            {
                return new Point3D();
            }
        }

        #endregion
    }
}
